package com.fpost.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.LookupDispatchAction;

import com.fpost.constants.ActionConstants;
import com.fpost.constants.CommonConstants;
import com.fpost.domain.User;
import com.fpost.form.LoginForm;
import com.fpost.form.dto.UserDTO;
import com.fpost.manager.UserManager;
import com.fpost.security.ActionSecurityUtils;
import com.fpost.security.CryptoUtils;
import com.fpost.util.ActionUtils;
import com.fpost.util.CommonUtils;

public class LoginAction extends LookupDispatchAction {
	
	private static Logger log = Logger.getLogger(LoginAction.class);
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		//set current path in session
		CommonUtils.setCurrentPathInSession(request, mapping);
		
		String method = request.getParameter("method");		
		if (method == null){
			return mapping.findForward(ActionConstants.SUCCESS);
		} 
		return super.execute(mapping, form, request, response);
	}
	
	protected Map<String, String> getKeyMethodMap() {
		
		Map<String, String> map = new HashMap<String, String>();		
		map.put("loginForm.login", "login");		
		map.put("loginForm.logout", "logout");	
		return map;		
	}
	
	public ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String forward = ActionConstants.SUCCESS;		
		try {			
			LoginForm loginForm = (LoginForm) form;

			String username = loginForm.getUsername();
			String password = loginForm.getPassword();
			
			User user = new UserManager().getByUsername(username);
			if (user != null && CryptoUtils.getHash(password).equals(user.getPassword())) {			
				UserDTO userDTO = new UserDTO();
				userDTO.setUserId(String.valueOf(user.getUserId()));
				userDTO.setUsername(user.getUsername());
				userDTO.setFirstName(user.getFirstName() == null ? "" : user.getFirstName());
				userDTO.setLastName(user.getLastName());
				
				ActionSecurityUtils.putUserDTO_inSession(userDTO, request);				
				forward = ActionConstants.SUCCESS;
			} else {
				ActionUtils.setErrorMessage(request, "error.login.failed");			
				forward = ActionConstants.FAILURE;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error(e.getMessage());
			forward = ActionConstants.SYSTEM_FAILURE;
		} 
		
		return mapping.findForward(forward);
	}
	
	public ActionForward logout(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		request.getSession().invalidate();
		return mapping.findForward(ActionConstants.SUCCESS);
	}

}
